package com.rowline.admin.module.study.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * ClassName:ThreadSoftedTest02
 * Created by 245167
 * on 2018/1/9  09:15
 * description:
 */
public class ThreadSoftedTest02 {

    public static void main(String[] args) {
        final Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " run 1");
            }
        },"T1");

        final Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " run 2");
                try{
                    t1.join(10);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        },"T2");

        final Thread t3 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + " run 3");
                try{
                    t2.join(10);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        },"T3");

        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(t1);
        executor.submit(t2);
        executor.submit(t3);
        executor.shutdown();

    /*    SingleThreadExecutor的工作线程只有一个，其他队列中的线程都处于休眠，也就是sleep状态，
        当这个worker线程做完事了，也就是run方法运行结束，
        就又从队列中拿出一个休眠线程（sleep）出来唤醒（notify），
        这样依次把队列中的所有线程处理完毕，这样并没有结束，
        如果线程池中没有待处理的线程，线程池一直会等待，等待下一次任务的提交，
        除非把线程池给shutdown掉，这样线程池的生命周期才算完毕。*/

    }

}
